package io.bloom.webspider;

import lombok.extern.slf4j.Slf4j;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Slf4j
public class SpiderDuplicateChecker {
    private static final Object EXIST = new Object();

    private LRUCache cache = new LRUCache(1000);

    public boolean isDuplicate(String link) {
        return EXIST == cache.get(link);
    }


    public void filter(List<String> list) {
        if (list == null) return;

        Iterator<String> iterator = list.iterator();
        while (iterator.hasNext()) {
            String link = iterator.next();
            if (isDuplicate(link)) {
                log.debug("link was duplicated {}", link);
                iterator.remove();
            }
        }
    }

    public void put(String link) {
        cache.put(link, EXIST);
    }

    private class LRUCache extends LinkedHashMap<String, Object> {
        private final int MAX_CACHE_SIZE;

        public LRUCache(int cacheSize) {
            super((int) Math.ceil(cacheSize / 0.75) + 1, 0.75f, true);
            MAX_CACHE_SIZE = cacheSize;
        }

        @Override
        protected boolean removeEldestEntry(Map.Entry eldest) {
            return size() > MAX_CACHE_SIZE;
        }

        @Override
        public String toString() {
            StringBuilder sb = new StringBuilder();
            for (Map.Entry<String, Object> entry : entrySet()) {
                sb.append(String.format("%s:%s ", entry.getKey(), entry.getValue()));
            }
            return sb.toString();
        }
    }
}
